package com.apache.uct.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.uct.common.entity.Dept;
import com.apache.uct.manager.DeptManager;
import com.apache.uct.util.UctStringUtil;

public class DeptManagerImpl implements DeptManager {

	protected IDao deptDao;
	protected final String entityName = "com.apache.uct.common.entity.Dept";

	public void setDeptDao(IDao deptDao) {
		this.deptDao = deptDao;
	}

	public String saveInfo(ParamsVo<Dept> vo) throws BusinessException {
		Dept dept = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="dept_"+infoId;
		dept.setDeptId(infoId);
		MethodParam param = new MethodParam("Dept", cacheKey, "", entityName);
		param.setVaule(dept);
		if (deptDao.insert(param)) {
			updateSubNum(dept.getFatherId(), "add");
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<Dept> vo) throws BusinessException {
		Dept dept = vo.getObj();
		if (Validator.isNotNull(dept.getDeptId())) {
			String cacheKey = "";
			//String cacheKey="dept_"+dept.getDeptId();
			MethodParam param = new MethodParam("Dept", cacheKey, "", entityName);
			param.setVaule(dept);
			return deptDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<Dept> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="dept_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		Dept info = (Dept) deptDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		boolean flag = false;
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("Dept");
			flag = deptDao.edit(param);
		} else {
			param.setParams("deptId", infoId);
			param.setDelete(true);
			flag = deptDao.delete(param);
		}
		if (flag) {
			updateSubNum(info.getFatherId(), "del");//更新子节点数
		}
		return flag;
	}

	//修改子节点数
	private void updateSubNum(String deptId, String flag) {
		if (!"0".equals(deptId)) {
			MethodParam param = new MethodParam("ById", "", "", entityName);
			param.setInfoId(deptId);
			Dept dept = (Dept) deptDao.selectById(param);
			int subNum = 0;
			if (null != dept.getSubCount())
				subNum = dept.getSubCount();
			if ("add".equals(flag)) {
				dept.setSubCount(subNum + 1);
			} else if ("del".equals(flag)) {
				dept.setSubCount(subNum - 1);
			}
			param = new MethodParam("Dept", "", "", entityName);
			param.setVaule(dept);
			deptDao.edit(param);
		}
	}

	public Object getInfoById(ParamsVo<Dept> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="dept_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return deptDao.selectById(param);
	}

	public Object execute(ParamsVo<Dept> vo) {
		String key = vo.getKey();
		if ("checkCodeName".equals(key)) {
			Dept dept = vo.getObj();
			return getCheckCodeName(dept.getDeptEname(), dept.getOrgId());
		} else if ("ByFatherId".equals(key)) {//
			return getFatherIdOrDeptId(vo);
		} else if ("ByProperty".equals(key)) {
			return getDeptByProperty(vo);
		}
		return null;
	}

	private Dept getDeptByProperty(ParamsVo<Dept> vo) {
		String propertyName = String.valueOf(vo.getParams("propertyName"));
		String propertyValue = String.valueOf(vo.getParams("propertyValue"));
		MethodParam param = new MethodParam("ByProperty", "", "", entityName);
		param.setParams(propertyName, propertyValue);
		if ("deptEname".equals(propertyName)) {
			String orgId = String.valueOf(vo.getParams("orgId"));
			if (Validator.isNotNull(orgId)) {//按机构ID查
				param.setParams("orgId", orgId);
				param.setSqlStr(deptDao.getSql(2) + " and " + propertyName + "=:" + propertyName + " and orgId=:orgId");
			} else {//按机构标识查
				String orgEname = String.valueOf(vo.getParams("orgEname"));
				param.setParams("orgEname", orgEname);
				param.setSqlStr(deptDao.getSql(2) + " and " + propertyName + "=:" + propertyName
						+ " and orgEname=:orgEname");
			}
		} else {
			param.setSqlStr(deptDao.getSql(2) + " and " + propertyName + "=:" + propertyName);
		}
		return (Dept) deptDao.selectSingle(param);
	}

	private List getFatherIdOrDeptId(ParamsVo<Dept> vo) {
		String fatherId = String.valueOf(vo.getParams("fatherId"));
		MethodParam param = new MethodParam("ByFatherIdOrDeptId", "", deptDao.getSql(5), entityName);
		if (Validator.isNotNull(fatherId)) {
			param.setParams("fatherId", fatherId);
			param.setSqlStr(deptDao.getSql(4));
		}
		param.setParams("randTime", System.currentTimeMillis() + "");
		param.setParams("userId", String.valueOf(vo.getParams("userId")));
		param.setParams("dataName", String.valueOf(vo.getParams("dataName")));
		return deptDao.select(param);
	}

	private String getCheckCodeName(String codeName, String orgId) {
		if (Validator.isNull(codeName)) {
			return "部门标识为空";
		}
		MethodParam param = new MethodParam("ByDeptEname", "", deptDao.getSql(3), entityName);
		param.setParams("deptEname", codeName);
		param.setParams("orgId", orgId);
		long count = deptDao.count(param);
		if (count > 0)
			return "部门标识已存在,请重新输入";
		return "";
	}

	public Page getPageInfo(ParamsVo<Dept> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "create_time";
		MethodParam param = setMethodParams(vo, 2, orderBy);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		param.setOrderby(orderBy + " desc");
		Page page = deptDao.pageSelect(param);
		return page;
	}

	public List<Dept> getList(ParamsVo<Dept> vo) {
		String orderBy = vo.getKey();
		if (Validator.isNull(orderBy))
			orderBy = "create_time";
		MethodParam param = setMethodParams(vo, 2, orderBy);
		param.setOrderby(orderBy + " desc");
		return deptDao.select(param);
	}

	public long countInfo(ParamsVo<Dept> vo) {
		MethodParam param = setMethodParams(vo, 1, "");
		return deptDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<Dept> vo, int type, String orderBy) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(deptDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		Dept dept = vo.getObj();
		if (Validator.isNotNull(dept.getSysEname())) {
			sb.append(" and sysEname =:sysEname");
			param.setParams("sysEname", dept.getSysEname());
		}
		if (Validator.isNotNull(dept.getOrgId())) {
			sb.append(" and orgId =:orgId");
			param.setParams("orgId", dept.getOrgId());
		}
		if (Validator.isNotNull(dept.getDeptId())) {
			sb.append(" and deptId =:deptId");
			param.setParams("deptId", dept.getDeptId());
		}
		if (Validator.isNotNull(dept.getOrgEname())) {
			sb.append(" and orgEname =:orgEname");
			param.setParams("orgEname", dept.getOrgEname());
		}
		if (Validator.isNotNull(dept.getDeptCode())) {
			sb.append(" and deptCode =:deptCode");
			param.setParams("deptCode", dept.getDeptCode());
		}
		if (Validator.isNotNull(dept.getDeptEname())) {
			sb.append(" and deptEname =:deptEname");
			param.setParams("deptEname", dept.getDeptEname());
		}
		if (Validator.isNotNull(dept.getFullEname())) {
			sb.append(" and fullEname =:fullEname");
			param.setParams("fullEname", dept.getFullEname());
		}
		if (Validator.isNotNull(dept.getFullCname())) {
			sb.append(" and fullCname =:fullCname");
			param.setParams("fullCname", dept.getFullCname());
		}
		if (Validator.isNotNull(dept.getDeptCname())) {
			sb.append(" and deptCname =:deptCname");
			param.setParams("deptCname", dept.getDeptCname());
		}
		if (Validator.isNotNull(dept.getFatherId())) {
			sb.append(" and fatherId =:fatherId");
			param.setParams("fatherId", dept.getFatherId());
		}
		if (Validator.isNotNull(dept.getDeptRemark())) {
			sb.append(" and deptRemark =:deptRemark");
			param.setParams("deptRemark", dept.getDeptRemark());
		}
		if (Validator.isNotNull(dept.getCreateUser())) {
			sb.append(" and createUser =:createUser");
			param.setParams("createUser", dept.getCreateUser());
		}
		if (Validator.isNotNull(dept.getUpdateUser())) {
			sb.append(" and updateUser =:updateUser");
			param.setParams("updateUser", dept.getUpdateUser());
		}
		if (Validator.isNotNull(orderBy)) {
			sb.append(" order by " + UctStringUtil.toCamelNamed(orderBy) + " desc");
		}
		param.setSqlStr(sb.toString());
		return param;
	}
}
